home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
ddj_cspc.arc
/
LADD.LST
< prev
next >
Wrap
File List
|
1989-12-13
|
12KB
|
496 lines
_C++ STRING CLASSES_
by Scott Robert Ladd
[LISTING ONE]
// Header: String (Dynamic Strings)
// Version: 1.01 13-Sep-1989
// Language: C++ 2.0
// Environ: Any
// Compilers: Zortech C++
// Purpose: Provides a general dynamic string class.
// Written by: Scott Robert Ladd
// 705 West Virginia
// Gunnison CO 81230
// MCI ID: srl
// FidoNet: 1:104/45.2
#if !defined(STRING_HPP)
#define STRING_HPP
#include "stddef.h"
class String
{
private:
// instance variables
unsigned int Siz; // allocated size
unsigned int Len; // current length
char * Txt; // pointer to text
// class constant
static unsigned int AllocIncr;
// private method used to shrink a string to its minimum allocation
void Shrink();
public:
enum StrCompVal {SC_LESS, SC_EQUAL, SC_GREATER};
enum StrCompMode {SF_SENSITIVE, SF_IGNORE};
// constructor
String();
String(String & Str);
String(char * Cstr);
String(char FillCh, unsigned int Count);
// destructor
~String();
// value return methods
unsigned int Length();
unsigned int Size();
// Function to return a blank string
friend String Empty();
// copy String to c-string method
void Copy(char * Cstr, unsigned int Max);
// create a c-string from String method
char * Dupe();
// assignment method
void operator = (String & Str);
// concatenation methods
friend String operator + (String Str1, String Str2);
void operator += (String Str);
// comparison method
StrCompVal Compare(String Str, StrCompMode Case = SF_IGNORE);
// substring search methods
int Find(String Str, unsigned int & Pos, StrCompMode Case = SF_IGNORE);
// substring deletion method
void Delete(unsigned int Pos, unsigned int Count);
// substring insertion methods
void Insert(unsigned int Pos, char Ch);
void Insert(unsigned int Pos, String Str);
// substring retrieval method
String SubStr(unsigned int Start, unsigned int Count);
// character retrieval method
char operator [] (unsigned int Pos);
// case-modification methods
String ToUpper();
String ToLower();
};
#endif
[LISTING TWO]
// Module: String (Dynamic Strings)
// Version: 1.01 13-Sep-1989
// Language: C++ 2.0
// Environ: Any
// Compilers: Zortech C++
// Purpose: Provides a general dynamic string class.
// Written by: Scott Robert Ladd
// 705 West Virginia
// Gunnison CO 81230
// MCI ID: srl
// FidoNet: 1:104/45.2
#include "String.hpp"
#include "string.h"
#include "stddef.h"
#include "ctype.h"
// class-global constant intialization
unsigned int String::AllocIncr = 8;
// private function to shrink the size of an allocated string
void String::Shrink()
{
char * Temp;
if ((Siz - Len) > AllocIncr)
{
Siz = ((Len + AllocIncr - 1) / AllocIncr) * AllocIncr;
Temp = new char[Siz];
memcpy(Temp,Txt,Len);
delete Txt;
Txt = Temp;
}
}
// constructor
String::String()
{
Len = 0;
Siz = AllocIncr;
Txt = new char[Siz];
Txt[0] = '\x00';
}
String::String(String & Str)
{
Len = Str.Len;
Siz = Str.Siz;
Txt = new char[Siz];
memcpy(Txt,Str.Txt,Len);
}
String::String(char * Cstr)
{
Len = strlen(Cstr);
Siz = ((Len + AllocIncr - 1) / AllocIncr) * AllocIncr;
Txt = new char[Siz];
memcpy(Txt,Cstr,Len);
}
String::String(char FillCh, unsigned int Count)
{
unsigned int Pos;
Siz = ((Count + AllocIncr - 1) / AllocIncr) * AllocIncr;
Len = Siz;
Txt = new char[Siz];
memset(Txt,FillCh,Count);
}
// destructor
String::~String()
{
delete Txt;
}
// value return methods
unsigned int String::Length()
{
return Len;
}
unsigned int String::Size()
{
return Siz;
}
// Function to return a blank string
String Empty()
{
static String EmptyStr;
return EmptyStr;
}
// copy String to c-string method
void String::Copy(char * Cstr, unsigned int Max)
{
unsigned int CopyLen;
if (Max == 0)
return;
if (Len >= Max)
CopyLen = Max - 1;
else
CopyLen = Len;
memcpy(Cstr,Txt,CopyLen);
Cstr[CopyLen] = '\x00';
}
// create a c-string from String method
char * String::Dupe()
{
char * new_cstr;
new_cstr = new char[Len + 1];
memcpy(new_cstr,Txt,Len);
new_cstr[Len] = '\x00';
return new_cstr;
}
// assignment method
void String::operator = (String & Str)
{
Len = Str.Len;
Siz = Str.Siz;
delete Txt;
Txt = new char[Siz];
memcpy(Txt,Str.Txt,Len);
}
// concatenation methods
String operator + (String Str1, String Str2)
{
unsigned int NewLen, NewSiz, CopyLen;
String TempStr;
char * Temp;
TempStr = Str1;
CopyLen = Str2.Len;
NewLen = TempStr.Len + Str2.Len;
NewSiz = TempStr.Siz + Str2.Siz;
Temp = new char[NewSiz];
memcpy(Temp,TempStr.Txt,TempStr.Len);
delete TempStr.Txt;
TempStr.Txt = Temp;
memcpy(&TempStr.Txt[TempStr.Len],Str2.Txt,CopyLen);
TempStr.Len = NewLen;
TempStr.Siz = NewSiz;
TempStr.Shrink();
return TempStr;
}
void String::operator += (String Str)
{
unsigned int NewLen, NewSiz, CopyLen;
char * Temp;
CopyLen = Str.Len;
NewLen = Len + CopyLen;
NewSiz = Siz + Str.Siz;
Temp = new char[NewSiz];
memcpy(Temp,Txt,Len);
delete Txt;
Txt = Temp;
memcpy(&Txt[Len],Str.Txt,CopyLen);
Len = NewLen;
Siz = NewSiz;
Shrink();
}
// comparison method
StrCompVal String::Compare(String Str, StrCompMode Case)
{
char * Temp1, * Temp2;
Temp1 = new char[Len + 1];
Copy(Temp1,Len+1);
Temp2 = new char[Str.Len + 1];
Str.Copy(Temp2,Str.Len+1);
if (Case == SF_IGNORE)
{
strupr(Temp1);
strupr(Temp2);
}
switch (strcmp(Temp1,Temp2))
{
case -1: return SC_LESS;
case 0: return SC_EQUAL;
case 1: return SC_GREATER;
}
delete Temp1;
delete Temp2;
}
// substring search methods
int String::Find(String Str, unsigned int & Pos, StrCompMode Case)
{
char * TempStr1, * TempStr2;
unsigned int LastPos, SearchLen, TempPos;
int Found;
TempStr1 = new char[Len + 1];
memcpy(TempStr1,Txt,Len);
TempStr1[Len] = '\x00';
TempStr2 = new char[Str.Len + 1];
memcpy(TempStr2,Str.Txt,Str.Len);
TempStr2[Str.Len] = '\x00';
if (Case == SF_IGNORE)
{
strupr(TempStr1);
strupr(TempStr2);
}
Pos = 0;
TempPos = 0;
Found = 0;
SearchLen = Str.Len;
LastPos = Len - SearchLen;
while ((TempPos <= LastPos) && !Found)
{
if (0 == strncmp(&TempStr1[TempPos],TempStr2,SearchLen))
{
Pos = TempPos;
Found = 1;
}
else
++TempPos;
}
delete TempStr1;
delete TempStr2;
return Found;
}
// substring deletion method
void String::Delete(unsigned int Pos, unsigned int Count)
{
unsigned int CopyPos;
if (Pos > Len)
return;
CopyPos = Pos + Count;
if (CopyPos >= Len)
Txt[Pos] = 0;
else
while (CopyPos <= Len)
{
Txt[Pos] = Txt[CopyPos];
++Pos;
++CopyPos;
}
Len -= Count;
Shrink();
}
// substring insertion methods
void String::Insert(unsigned int Pos, char Ch)
{
char * Temp;
if (Pos > Len)
return;
if (Len == Siz)
{
Siz += AllocIncr;
Temp = new char[Siz];
memcpy(Temp,Txt,Len);
delete Txt;
Txt = Temp;
}
if (Pos < Len)
for (unsigned int Col = Len + 1; Col > Pos; --Col)
Txt[Col] = Txt[Col-1];
Txt[Pos] = Ch;
++Len;
}
void String::Insert(unsigned int Pos, String Str)
{
unsigned int SLen = Str.Len;
SLen = Str.Len;
if (SLen > 0)
for (unsigned int I = 0; I < SLen; ++I)
{
Insert(Pos,Str.Txt[I]);
++Pos;
}
}
// substring retrieval method
String String::SubStr(unsigned int Start, unsigned int Count)
{
String TempStr;
char * Temp;
if ((Start < Len) && (Count > 0))
for (unsigned int Pos = 0; Pos < Count; ++Pos)
{
if (TempStr.Len == TempStr.Siz)
{
TempStr.Siz += AllocIncr;
Temp = new char[TempStr.Siz];
memcpy(Temp,TempStr.Txt,Len);
delete TempStr.Txt;
TempStr.Txt = Temp;
}
TempStr.Txt[Pos] = Txt[Start + Pos];
++TempStr.Len;
}
return TempStr;
}
// character retrieval method
char String::operator [] (unsigned int Pos)
{
if (Pos >= Len)
return '\x00';
return Txt[Pos];
}
// case-modification methods
String String::ToUpper()
{
String TempStr = *this;
for (unsigned int Pos = 0; Pos < Len; ++Pos)
TempStr.Txt[Pos] = toupper(TempStr.Txt[Pos]);
return TempStr;
}
String String::ToLower()
{
String TempStr = *this;
for (unsigned int Pos = 0; Pos < Len; ++Pos)
TempStr.Txt[Pos] = tolower(TempStr.Txt[Pos]);
return TempStr;
}
[LISTING THREE]
#include "String.hpp"
#include "stdio.h"
#include "stream.hpp"
int main();
void print_string(String S);
String s1;
String s2("This is the second string!");
int main()
{
String ls;
String ls2("Another local string");
unsigned int pos, i;
char ch;
s1 = s2;
ls = "This is the local string.";
print_string(s1);
print_string(s2);
print_string(ls);
print_string(ls2);
cout << "\n";
s1 = s2 + ls;
print_string(s1);
s1 = "String one has a value.";
print_string(s1);
s1 = s1 + "****";
print_string(s1);
s2 += "*****";
print_string(s2);
print_string(ls);
s2 += ls;
print_string(s2);
cout << "\n";
if (s2.Find("Burfulgunk",pos))
printf("first search = %d\n",pos);
if (s2.Find("*****",pos))
printf("second search = %d\n",pos);
if (s2.Find(ls,pos))
printf("third search = %d\n",pos);
if (s2.Find(s1,pos))
printf("fourth search = %d\n",pos);
ls2 = "&&";
s1.Insert(10,'*');
s1.Insert(15,ls2);
print_string(s1);
s1.Insert(s1.Length(),'%');
s1.Insert(s1.Length(),'%');
s1.Insert(s1.Length(),'%');
s1.Insert(s1.Length(),'%');
print_string(s1);
for (i = 0; 0 != (ch = s1[i]); ++i)
putchar(ch);
putchar('\n');
s1.Insert(2,"<><><><><>");
print_string(s1);
s1.Delete(2,10);
print_string(s1);
s2 = s1.ToUpper();
print_string(s2);
s2 = s1.ToLower();
print_string(s2);
s1 = Empty();
print_string(s1);
s1 = s2.SubStr(2,10);
print_string(s1);
return 0;
}
void print_string(String S)
{
char * cs;
cs = S.Dupe();
cout << cs << " Len = " << S.Length() << " Siz = " << S.Size() << "\n";
delete cs;
}